/*
* tracker.c
*
* Created: 29.10.2012 12:47:26
* Author: ivovk, mvchurik
*/

#define F_CPU 8000000UL

#include <util/delay.h>
#include <avr/interrupt.h>
#include <avr/io.h>
#include <stdbool.h>
#include <avr/pgmspace.h>
#include "lcd.h"
#include "cust_chars.h"

#include <stdlib.h>

#define USART_BAUDRATE1 115200//Bits per second
#define UBRR_VALUE1 ((F_CPU/(USART_BAUDRATE1 * 16UL)) - 1)//Asynchronous Normal Mode

#define USART_BAUDRATE0 38400//Bits per second
#define UBRR_VALUE0 ((F_CPU/(USART_BAUDRATE0 * 16UL)) - 1)//Asynchronous Normal Mode

#define PIN 0000
#define TRY 10
#define GSM_CNTRL_MASK 0x78 //1<<PD3|1<<PD4|1<<PD5|1<<PD6 Mask selects CTS,DTR,DSR,DCD
#define RXBUFLENGTH_GSM 200
#define PRECISION 1
#define NUMBER "+79161307706"


typedef struct{//Size = 60 byte
	unsigned char timestamp[6];	//hhmmss
	unsigned char lat[10];	//DDMM.MMMMM
	unsigned char n_s[1];	//N/w
	unsigned char longi[10];	//DDMM.MMMMM
	unsigned char e_w[1];	//E/W
	unsigned char gps_qual[1];	//d - 0,1,2
	unsigned char sats[2];	//dd
	unsigned char hdop[4];	//dd.d
	unsigned char alt[6];	//mmm.ss
	unsigned char speed[5];	//kkk.m
	unsigned char date[6];	//ddmmyy
}gps_data;

char* GPGGA = "GPGGA";
char* GPRMC = "GPRMC";
gps_data cur_g_data;//Global variable contains current gps data

uint8_t nmea_header[5];
uint8_t nmea_last_gga[80];
uint8_t nmea_last_rmc[80];
uint8_t nmea_header_position = 0;
uint8_t nmea_rmc_ptr = 0;
uint8_t nmea_gga_ptr = 0;
bool uart1_msg_started = false, uart1_rmc_recv = false, uart1_gga_recv = false;

volatile char RX_buf[RXBUFLENGTH_GSM];//Buffer for data from gsm
volatile uint8_t RxBufPoint = 0;
uint8_t timer0_counter = 0;

uint32_t* key = (uint32_t*)"THEKEY!!";

static unsigned char const k8[16] = {
14,  4, 13,  1,  2, 15, 11,  8,  3, 10,  6, 12,  5,  9,  0,  7 };
static unsigned char const k7[16] = {
15,  1,  8, 14,  6, 11,  3,  4,  9,  7,  2, 13, 12,  0,  5, 10 };
static unsigned char const k6[16] = {
10,  0,  9, 14,  6,  3, 15,  5,  1, 13, 12,  7, 11,  4,  2,  8 };
static unsigned char const k5[16] = {
7, 13, 14,  3,  0,  6,  9, 10,  1,  2,  8,  5, 11, 12,  4, 15 };
static unsigned char const k4[16] = {
2, 12,  4,  1,  7, 10, 11,  6,  8,  5,  3, 15, 13,  0, 14,  9 };
static unsigned char const k3[16] = {
12,  1, 10, 15,  9,  2,  6,  8,  0, 13,  3,  4, 14,  7,  5, 11 };
static unsigned char const k2[16] = {
4, 11,  2, 14, 15,  0,  8, 13,  3, 12,  9,  7,  5, 10,  6,  1 };
static unsigned char const k1[16] = {
13,  2,  8,  4,  6, 15, 11,  1, 10,  9,  3, 14,  5,  0, 12,  7 };

//===================================
//Useful functions
//===================================

void clr_chars(unsigned char *p, uint8_t len){
	int i = 0;
	for(i = 0; i < len; i++){
		p[i] = '0';
	}
}

/**
Compare of two strings, but only N first symbols
returns 1 if these strings are similar and 0 if not
*/
bool compare_st(unsigned char* one, unsigned char *two, uint8_t N){
	int i;
	for(i = 0; i < N; i++){
		if (one[i] != two[i]){
			return false;
		}
	}
	return true;	
}

/**
This function copy N characters from source to dest
*/
void refill_str(unsigned char* dest, unsigned char* source, uint8_t N){
	uint8_t i;
	for(i = 0; i < N; i++){
		dest[i] = source[i];
	}
}

/**
This function copy token(between ',') numbered <num> from <source> to <dest>.
Token number starts from 1
*/
int copy_token(uint8_t* dest, uint8_t* source , uint8_t num){
	uint8_t counter = 0;
	uint8_t j;
	uint8_t last_start = 0;
	uint8_t len = 80;
	
	for(j = 0; j < len; j++){
		if( source[j] == ','){
			if( counter == num ){
				refill_str(dest, source+last_start, j - last_start);
				return j - last_start;
			}else{
				counter++;
				last_start = j + 1;
			}
		}							
	}
	return -1;
}

/**
This function clears Rx buffer of USART0 (GSM)
*/
void ClearRxBuf(){
	uint8_t i;
	for (i = 0; i < RXBUFLENGTH_GSM; i++){
		RX_buf[i] = 0;
	}		
	RxBufPoint = 0;
	PORTD &= ~(1<<PD2);//Set RTS = '0' to inform GSM ATMEGA ready for data receiving
}

/*
 * Do the substitution and rotation that are the core of the operation,
 * like the expansion, substitution and permutation of the DES.
 * It would be possible to perform DES-like optimisations and store
 * the table entries as 32-bit words, already rotated, but the
 * efficiency gain is questionable.
 *
 * This should be inlined for maximum speed
 */
uint32_t f(uint32_t x)
{
	x = k8[x>>28 & 15] << 28 |
		k7[x>>24 & 15] << 24 |
		k6[x>>20 & 15] << 20 |
		k5[x>>16 & 15] << 16 |
		k4[x>>12 & 15] << 12 |
		k3[x>>8 & 15]  << 8  |
		k2[x>>4 & 15]  << 4  |
		k1[x & 15];
	/* Do substitutions */
	/* This is faster */
	// x = k87[x>>24 & 255] << 24 | k65[x>>16 & 255] << 16 |
	//     k43[x>> 8 & 255] <<  8 | k21[x & 255];
	/* Rotate left 11 bits */
	return x<<11 | x>>(32-11);
}

/*
 * The GOST standard defines the input in terms of bits 1..64, with
 * bit 1 being the lsb of in[0] and bit 64 being the msb of in[1].
 *
 * The keys are defined similarly, with bit 256 being the msb of key[7].
 */
void gostcrypt(uint32_t const in[2], uint32_t out[2], uint32_t const key[8])
{
	register uint32_t n1, n2; /* As named in the GOST */
	uint32_t mask = 0x1;
	int i,j;

	n1 = in[0];
	n2 = in[1];

	/* Instead of swapping halves, swap names each round */
    for (j = 0; j < 3; j++){
        for (i = 0; i < 8; i++){
            if ( (i & mask) == 0 ) //Even
                n2 ^= f(n1 + key[i]);
            if ( (i & mask) == 1 ) //Even
                n1 ^= f(n2 + key[i]);
        }
    }

    for (i = 7; i >= 0; i--){
        if ( (i & mask) == 1 ) //Even
            n2 ^= f(n1 + key[i]);
        if ( (i & mask) == 0 ) //Even
            n1 ^= f(n2 + key[i]);
    }
	/* There is no swap after the last round */
	out[0] = n2;
	out[1] = n1;
}

void encrypt(char* message, char* res, uint16_t len){
	uint8_t num_8 = len/8;
	uint8_t i;
	
	for(i = 0; i < num_8; i++){
		gostcrypt((uint32_t*)(message+(i*8)), (uint32_t*)(res+(i*8)), key);
	}
}

//===================================
//Hardware initialization and reset
//===================================

/**
Procedure of hardware initialization of atmega controller
*/
void initialization(){
	//Initialization of port B (GPS)
	DDRB = 0x9;// PB3 and PB0 set as out;
	PORTB = 0x1;//Set HIGH level on /reset pin (PB0)
	//TX1 and RX1 pins are Z inputs
	
	//Initialization USART1 (GPS)
	// Set baud rate
	UBRR1H = (uint8_t)(UBRR_VALUE1>>8);
	UBRR1L = (uint8_t)(UBRR_VALUE1);
	// Set frame format to 8 data bits, no parity, 1 stop bit
	UCSR1C |= (1<<URSEL1)|(1<<UCSZ11)|(1<<UCSZ10);
	//enable transmission and reception and receive interrupt
	UCSR1B |= (1<<RXEN1)|(1<<TXEN1)|(1<<RXCIE1);
	
	//Initialization of port D and E (GSM)
	DDRD = 0x16;// PD1 and PD2 and PD4 are out (TX, RTS, DTR)
	DDRE = 0x3;//PE0 and PE1 are out (Reset_gsm, IGN)
	PORTD = (1<<PD2);//RTS is high. Tells gsm that ATMEGA UART not ready
	PORTE = 0;//No HIGH levels on PORTE
	//TX0 and RX0 pins are Z inputs
	
	//Initialization USART0 (GSM)
	// Set baud rate
	UBRR0H = (uint8_t)(UBRR_VALUE0>>8);
	UBRR0L = (uint8_t)(UBRR_VALUE0);
	// Set frame format to 8 data bits, no parity, 1 stop bit
	UCSR0C |= (1<<URSEL0)|(1<<UCSZ01)|(1<<UCSZ00);
	//enable transmission and reception and NOT receive interrupt
	UCSR0B |= (1<<RXEN0)|(1<<TXEN0)|(1<<RXCIE0);
	
	ClearRxBuf();//Clearing RX USART0 buffer and set RTS = '0'
	PORTD |= (1<<PD4);//DTR = '1'. It says to GSM chip that atmega ready
	
	// Timer/Counter 1 initialization
	// Clock source: System Clock
	// Clock value: 7,813 kHz
	// Mode: Normal top=0xFFFF
	// OC1A output: Discon.
	// OC1B output: Discon.
	// Noise Canceler: Off
	// Input Capture on Falling Edge
	// Timer1 Overflow Interrupt: On
	// Input Capture Interrupt: Off
	// Compare A Match Interrupt: Off
	// Compare B Match Interrupt: Off
	TCCR1A=0x00;
	TCCR1B=0x05;
	TCNT1H=0x00;
	TCNT1L=0x00;
	ICR1H=0x00;
	ICR1L=0x00;
	OCR1AH=0xFF;
	OCR1AL=0xFF;
	OCR1BH=0x00;
	OCR1BL=0x00;
	
	// Timer(s)/Counter(s) Interrupt(s) initialization
	TIMSK=0x80;
	
	//Initialization of lcd	
	lcd_init(LCD_DISP_ON);
	lcd_command(LCD_FUNCTION_4BIT_2LINES );
	lcd_custom_char(0, ant);
	lcd_custom_char(1, one);
	lcd_custom_char(2, two);
	lcd_custom_char(3, three);
	lcd_custom_char(4, four);
	lcd_custom_char(5, five);
	lcd_clrscr();
	
	//Clearing of gps data buffer
	clr_chars(cur_g_data.timestamp, 7);
	clr_chars(cur_g_data.lat, 11);
	clr_chars(cur_g_data.n_s, 2);
	clr_chars(cur_g_data.longi, 11);
	clr_chars(cur_g_data.e_w, 2);
	clr_chars(cur_g_data.gps_qual, 2);
	clr_chars(cur_g_data.hdop, 5);
	clr_chars(cur_g_data.alt, 7);
	clr_chars(cur_g_data.speed, 5);
	clr_chars(cur_g_data.date, 7);
}

/**
Send reset signal to GPS chip
*/
void reset_gps(){
	uint8_t UCSR1B_store = UCSR1B;//Save state of UCSR1B
	while( !( UCSR1A & (1<<UDRE1)) ){};//Wait for empty USART1 transmit buffer
	UCSR1B &= ~((1<<TXEN1)|(1<<RXEN1)|(1<<RXCIE1));//Disable USART1 transmitter and receiver {and interruption}

	DDRB = 0x9;// PB3 and PB0 set as out;
	PORTB = 0x1;//Set HIGH level on /reset pin (PB0)
	//TX1 pin is in Z state

	PORTB = 0;//Set LOW level on /reset pin (PB0) for 15ms
	_delay_ms(15);

	PORTB = 0x1;//Set HIGH level on /reset pin (PB0)
	_delay_ms(25);//Wait gps chip reset and set up to work mode

	UCSR1B = UCSR1B_store;//Restore state of UCSR1B
}

/**
Send emergency reset signal to GSM chip
*/
void e_reset_gsm(){
	uint8_t UCSR0B_store = UCSR0B;//Save state of UCSR0B
	while( !( UCSR0A & (1<<UDRE0)) ){};//Wait for empty USART1 transmit buffer
	UCSR0B &= ~((1<<TXEN0)|(1<<RXEN0)|(1<<RXCIE0));//Disable USART0 transmitter and receiver and interruption
	
	DDRD = 0x16;// PD1 and PD2 and PD4 are out (TX, RTS, DTR)
	DDRE = 0x3;//PE0 and PE1 are out (Reset_gsm, IGN)
	_delay_ms(1);
	
	PORTE |= 0x2; //Set '1' on Reset_gsm pin
	_delay_ms(15);
	
	PORTE &= ~0x2;//Set Reset_gsm pin back to '0'
	_delay_ms(500);//Wait 0.5s for GSM module set up
		
	UCSR0B = UCSR0B_store;//Restore state of UCSR0B
}

/**
Send ignition signal to GSM chip
*/
void gsm_on(){
	PORTE = 0x1;//Set PE0(IGN) to '1' for 15ms
	_delay_ms(15);
	PORTE = 0;//Set PORT E to '0'(IGN=0|R_GSM=0)
}


//===================================
//USART processing
//===================================

/**
Send 1 byte via USART1(GPS)
*/
void USART1_Send(uint8_t u8Data){
	//wait for empty transmit buffer and GSM is ready for send
	while( !( UCSR1A & (1<<UDRE1)) ){};
	// Put data into buffer, sends the data
	UDR1 = u8Data;
}

/**
Send 1 byte via USART0(GSM)
*/
void USART0_Send(uint8_t u8Data){
	//wait for empty transmit buffer and GSM is ready for send
	while( !( UCSR0A & (1 << UDRE0) ) ){};
	// Put data into buffer, sends the data
	UDR0 = u8Data;
}

/**
Send AT command to GSM module
*/
void Send_AT_cmd(char *cmd){
	while(*cmd){
		USART0_Send( (uint8_t)*cmd );
		cmd++;
		_delay_ms(50);
	}
}

//===================================
//Interruptions precessing
//===================================

ISR(USART0_RXC_vect){
	char InData = UDR0;//Read data from USART0 anyway
	if (InData > 20){
		RX_buf[RxBufPoint] = InData;//Store information
		RxBufPoint++;
	}			
	if (RxBufPoint > 155)
		PORTD |= (1<<PD2);//Set RTS='1' to inform GSM that buffer is near to overflow.
}

ISR(USART1_RXC_vect){
	char InData = UDR1; //Read data from USART1 anyway
    if (InData == '$') {
        uart1_msg_started = true;
        nmea_header_position = 0;

    } else if (uart1_msg_started) {

        if (uart1_rmc_recv||uart1_gga_recv) {

            if (uart1_rmc_recv) {
                nmea_last_rmc[nmea_rmc_ptr++] = InData;
            }

            if (uart1_gga_recv) {
                nmea_last_gga[nmea_gga_ptr++] = InData;
            }

            if (InData == '\n') {
                uart1_msg_started = false;
                uart1_rmc_recv = false;
                uart1_gga_recv = false;
            }

        } else {
            nmea_header[nmea_header_position++] = InData;

            if (nmea_header_position == 5) {
                if (compare_st(nmea_header, GPRMC, 5)) { // RMC
                    uart1_rmc_recv = true;
                    nmea_rmc_ptr = 0;
                } else if (compare_st(nmea_header, GPGGA, 5)) { // GGA
                    uart1_gga_recv = true;
                    nmea_gga_ptr = 0;
                } else uart1_msg_started = false;
            } 
        }
    }
}

ISR(__vector_default){
	asm("nop");
}

// TIMER1 interrupt
ISR(TIMER1_OVF_vect){
	if (timer0_counter < 14) timer0_counter++;
	//if (timer0_counter < 2) timer0_counter++;
	else {
		Send_SMS();
		timer0_counter = 0;
	}
}

//===================================
//GPS data processing
//===================================

void Lcd_print_GPS(){	
	
	lcd_gotoxy(0,1);
	lcd_puts((unsigned char *)"Lat.:");
	lcd_puts_len(cur_g_data.lat,10);
	lcd_puts_len(cur_g_data.n_s,1);
	
	_delay_ms(1000);
	lcd_gotoxy(0,1);
	lcd_puts("                ");
	lcd_gotoxy(0,1);
	lcd_puts((unsigned char *)"Lon.:");
	lcd_puts_len(cur_g_data.longi,10);
	lcd_puts_len(cur_g_data.e_w,1);
	
	_delay_ms(1000);
	lcd_gotoxy(0,1);
	lcd_puts("                ");
	lcd_gotoxy(0,1);
	lcd_puts((unsigned char *)"Speed:");
	//snprintf(speed, 6,"%5.1f",);
	lcd_puts(cur_g_data.speed);//NEED TO BE CHECKED!!!!
	
	_delay_ms(1000);
	lcd_gotoxy(0,1);
	lcd_puts("                ");
	lcd_gotoxy(0,1);
	lcd_puts("D:");
	lcd_puts_len(cur_g_data.date,6);
	lcd_puts("T:");
	lcd_puts_len(cur_g_data.timestamp,6);
}

/**
 This function gets nmea messages from USART1. 
There are two meanful messages:
GPGGA(5_ - position (with 5 signs after point), satellites, high(in meters) and hdop.
GPRMC - position (4 signs), speed and date.
It updates global variable cur_g_data according to these messages if there any.
*/
void upd_gps_store(){	
	float speed_num = 0.0;
	uint8_t speed_int = 0;
	char buf[5];
	/*$GPRMC,<Timestamp>,<Status>,<Lat>,<N/S>,<Long>,<E/W>,<Speed>,<Trackgood
	>,<Date>,<MagVar>,<MagVarDir> <checksum><cr><lf>*/
	copy_token(cur_g_data.timestamp, nmea_last_rmc, 1);
	copy_token(buf, nmea_last_rmc, 7);
	speed_num = atof(buf);
	speed_num *= 1.852;
	speed_int = trunc(speed_num);
	if( speed_int <= 2)
		speed_int = 0;
	clr_chars(cur_g_data.speed, 5);
	itoa(speed_int, cur_g_data.speed, 10);
	copy_token(cur_g_data.date, nmea_last_rmc, 9);
		
   	/*$GPGGA,<Timestamp>,<Lat>,<N/S>,<Long>,<E/W>,<GPSQual>,<Sats>,<HDOP>,<Alt>,
   	<AltVal>,<GEOSep>,<GEOVal>,<DGPSAge>,<DGPSRef>,<checksum><cr><lf>*/
	copy_token(cur_g_data.timestamp, nmea_last_gga, 1);
	copy_token(cur_g_data.lat, nmea_last_gga, 2);
	copy_token(cur_g_data.n_s, nmea_last_gga, 3);
	copy_token(cur_g_data.longi, nmea_last_gga, 4);
	copy_token(cur_g_data.e_w, nmea_last_gga, 5);
	copy_token(cur_g_data.gps_qual, nmea_last_gga, 6);
	copy_token(cur_g_data.sats, nmea_last_gga, 7);
	copy_token(cur_g_data.hdop, nmea_last_gga, 8);
	copy_token(cur_g_data.alt, nmea_last_gga, 9);		
}


//===================================
//GSM chip data processing
//===================================

/**
Check state of GSM chip
CTS - Clear to send. If '0' gsm ready to receive data via uart
DTR - Data terminal ready. If '1' amtega ready for transmission
DSR - Data set ready - Always '1' by default in gsm
DCD - Data carrier detect. '1' means that gsm detect gsm signal
*/
bool Gsm_Ready(){
	
	if( (PIND & GSM_CNTRL_MASK) == 0x70)//CTS==0|DTR==1|DSR==1|DCD==1
		return true;
	else
		return false;
}

/**
Set all settings to necessary in GSM chip
*/
//int set_settings_gsm(){
	//int out = 0;
	//sei();//Enable interruptions. Need for correct working of UART0(GSM) receiver
	//
	//ClearRxBuf();
	//Send_AT_cmd("ATE0\r");//Disable echo mode
	//while (RxBufPoint == 0){}
	//_delay_ms(500);
	//if (compare_st(RX_buf, "ATE0OK", 2) == false)
	//out = -1;
	//
	//ClearRxBuf();
	//Send_AT_cmd("AT+IPR=115200\r");//Setup fixed UART speed in gsm	
	//while (RxBufPoint == 0){}
	//_delay_ms(500);
	//if (compare_st(RX_buf, "OK", 2) == false)
		//out = -2;
//
	////ClearRxBuf();
	////Send_AT_cmd("AT+CLCK=", 0);//Disable PIN code blocking. <AT+CLCK="SC",0,"PIN">
	////Send_AT_cmd(quote, 0); 
	////Send_AT_cmd("SC", 0);
	////Send_AT_cmd(quote, 0);
	////Send_AT_cmd(",0,", 0);
	////Send_AT_cmd(quote, 0);
	////Send_AT_cmd(PIN, 0);
	////Send_AT_cmd(quote, 0);
	////Send_AT_cmd("\r", 1);
	////if (compare_st(RX_buf, "OK", 2) == false)
		 ////out = -3;
	//
	//ClearRxBuf();
	//Send_AT_cmd("AT+CMGF=1\r");//Set text mode for sms sending
	//while (RxBufPoint == 0){}
	//_delay_ms(500);
	//if (compare_st(RX_buf, "OK", 2) == false)
		//out = -4;
	////
	////ClearRxBuf();
	////Send_AT_cmd("AT^SSET=1\r", 1);//Enable URC <^SSIM READY>
	////_delay_ms(300);
	////if (compare_st(RX_buf, "OK", 2) == false)
		////out = -5;
	//
	//return out;
//}

void Send_SMS(void){
	char buf[72];
	char out[73];
		
	refill_str(buf, "Time: ",6);
	refill_str(&buf[6], cur_g_data.timestamp, 6);
	buf[12] = '\n';
	refill_str(&buf[13], "Date: ",6);
	refill_str(&buf[19], cur_g_data.date, 6);
	buf[25] = '\n';
	refill_str(&buf[26], "Lat.: ",6);
	refill_str(&buf[32], cur_g_data.lat, 10);
	buf[42] = '\n';
	refill_str(&buf[43], "Lon.: ",6);
	refill_str(&buf[49], cur_g_data.longi, 10);
	buf[59] = '\n';
	refill_str(&buf[60], "Speed: ",7);
	refill_str(&buf[67], cur_g_data.speed, 5);
	
	encrypt(buf, out, 72);
	out[72] = '\0';
	buf[72] = '\0';
		
	ClearRxBuf();
	Send_AT_cmd("AT+CMGF=1\r");
	_delay_ms(500);

	ClearRxBuf();
	Send_AT_cmd("AT+CMGS=");
	Send_AT_cmd(NUMBER);
	Send_AT_cmd("\r");
	_delay_ms(100);
	if(RxBufPoint){
		lcd_gotoxy(0,1);
		lcd_puts_len(&RX_buf[RxBufPoint-16], 16);
		_delay_ms(300);
	}

	ClearRxBuf();
	Send_AT_cmd(buf);
	//Send_AT_cmd(out);
	Send_AT_cmd("\032");
	if(RxBufPoint){
		lcd_gotoxy(0,1);
		lcd_puts_len(&RX_buf[RxBufPoint-16], 16);
		_delay_ms(300);
	}		
}

void show_signal(void){
	int strength = 0;
	
	ClearRxBuf();	
	//sei();
	Send_AT_cmd("AT+CSQ\r");
	_delay_ms(500);
	//cli();
	
	RX_buf[RxBufPoint-5] = '\0';
	strength = atoi(&RX_buf[RxBufPoint-7]);
	if (strength >= 25)
	strength = 5;
	else{
		strength /= 5;
		if (strength == 0) strength = 1;
	}
	if (strength == 99)
	strength = 0;
	
	lcd_gotoxy(0,0);
	lcd_data(0);
	lcd_data(strength);	
}

void show_operator(void){
	ClearRxBuf();
	Send_AT_cmd("AT+COPS?\r");
	_delay_ms(400);
	lcd_gotoxy(3,0);
	if (compare_st(&RX_buf[RxBufPoint-10], "+COPS:",6))
		lcd_puts_len("INS SIM",7);
	else
		lcd_puts_len(&RX_buf[RxBufPoint-10],7);
}

//===================================
//MAIN function
//===================================

int main(void){
	int i=0;
	char rx_point[4];
	clr_chars(rx_point, 4);
	
	cli();
		
	initialization();
	
	gsm_on();
	
	sei();
	
	lcd_puts("Welcome\nUser");
	_delay_ms(1000);
	lcd_clrscr();
	lcd_puts("Wait GSM");
	_delay_ms(2000);
	lcd_clrscr();
	
	uint32_t* key = (uint32_t*)"THEKEY!!";
	uint32_t* plain = (uint32_t*)"AB";
	uint32_t cipher[2];
	uint32_t result[2];	

	
	while(1){
		show_signal();
	    
	    _delay_ms(500);
	    
	    show_operator();
		_delay_ms(2000);
		
		upd_gps_store();
		Lcd_print_GPS();
		
		//Send_SMS();
		
		_delay_ms(1000);
    }
}
